var $searchBtn,
	$searchForm,
	$listPage,
	$list,
	$editItem,
	$editModal,
	$previousPage,
	$nextPage;

var RESULT,
	PARAM;

var renderList = function() {
	var skip = parseInt($searchForm.find('input.skip').val(), 10);
	var limit = parseInt($searchForm.find('input.limit').val(), 10);
	var nowPage = Math.floor(skip / PAGE.limit) + 1;
	var countPage = Math.ceil(RESULT.count / PAGE.limit);

	if (nowPage <= 1) {
		$previousPage.addClass('disabled');
	} else {
		$previousPage.removeClass('disabled');
	}
	if (nowPage >= countPage) {
		$nextPage.addClass('disabled');
	} else {
		$nextPage.removeClass('disabled');
	}

	$listPage.text(nowPage + '/' + countPage);
	$list.empty();
	$.each(RESULT.list, function(i, order) {

		var $order = $('<tr>' +
			'	<td>' + order.employer + '</td>' +
			'	<td>' + order.waiter + '</td>' +
			'	<td>' + $.format.date(new Date(order.createTime), 'yyyy-MM-dd HH:mm:ss') + '</td>' +
			// '	<td>' + order.createTime.toLocaleDateString() + '</td>' +
			'	<td>' + order.status + '</td>' +
	        '   <td>' +
	        '       <button class="btn btn-default btn-xs edit" type="button"><span class="glyphicon glyphicon-edit" aria-hidden="true"></span></button>' +
	        '   </td>' +
			'</tr>')
			.appendTo($list);
			$order.find('button.edit').click(function() {
				var $this = $(this);
				$editModal.find('input.id').val(order._id);
				$editModal.find('input.name').val(order.name);
				$editModal.find('input.phone').val(order.phone);
				$editModal.find('input.password').val('');
				$editModal.find('input.sex').each(function(i, radio) {
					radio.checked = order.sex == radio.value;
				});
				$editModal.find('input.idNum').val(order.idNum);
				$editModal.find('input.bankId').val(order.bankId);
				$editModal.find('input.bankName').val(order.bankName);
				$editModal.find('input.bankPerson').val(order.bankPerson);
				$editModal.find('input.hometown').val(order.hometown);
				$editModal.find('input.ageLevel').val(order.ageLevel);
				$editModal.find('input.empiricLevel').val(order.empiricLevel);
				$editModal.find('input.eduLevel').val(order.eduLevel);
				$editModal.find('input.wageLevel').val(order.wageLevel);
				$editModal.find('input.photo').val(order.photo);
				$editModal.find('img.photo').attr('src', '/entity/file/' + order.photo).show();
				$editModal.find('input.idCardFront').val(order.idCardFront),
				$editModal.find('img.idCardFront').attr('src', '/entity/file/' + order.idCardFront).show();
				$editModal.find('input.idCardBack').val(order.idCardBack)
				$editModal.find('img.idCardBack').attr('src', '/entity/file/' + order.idCardBack).show();
	            $.each(order.trades, function(i, trade) {
	                $editModal.find('input.trades[value="' + trade + '"]').each(function(j, checkbox) {
	                    checkbox.checked = true;
	                });
	            });
				$editModal.modal('show');
			});

	        $order.find('button.remove').click(function() {
	            if (window.confirm('是否要删除服务员“' + order.name + '”？')) {
	                deleteWaiter(order._id, function(err) {
	                    if (err) {
	                        alert('删除失败。');
	                    }

	                    $searchBtn.click();
	                });
	            }
	        });
	});
};

var init = function() {
	$searchBtn = $('.search-btn');
	$searchForm = $('.search-form');
	$listPage = $('.pager .page');
	$list = $('tbody.list');
	$editItem = $('.edit');
	$editModal = $('#edit_modal');
	$previousPage = $('li.previous');
	$nextPage = $('li.next');


    // 编辑模态窗口
    $editModal.on('hidden.bs.modal', function(event) { // 窗口隐藏
        $editModal.find('input:text').val('');
        $editModal.find('input[type="hidden"]').val('');
        $editModal.find('img').attr('src', '');
        $editModal.find('input:checkbox, input:radio').each(function(i, input) {
			input.checked = false;
        });
		$editModal.find('.has-error').removeClass('has-error');
		$editModal.find('.alert').hide();
    });

	// 查询
	$searchBtn.click(function(event) {
		$searchForm.find('input.skip').val(PAGE.skip);
		$searchForm.find('input.limit').val(PAGE.limit);
		PARAM = $('form.search-form').serialize();
		getOrders(PARAM, function(err, result) {
			RESULT = result;
			renderList();
		});
	}).click();

	$previousPage.click(function() {
		var skip = parseInt($searchForm.find('input.skip').val(), 10);
		var limit = parseInt($searchForm.find('input.limit').val(), 10);
		skip -= limit;
		$searchForm.find('input.skip').val(skip);
		$searchForm.find('input.limit').val(limit);
		PARAM = $('form.search-form').serialize();
		getOrders(PARAM, function(err, result) {
			RESULT = result;
			renderList();
		});

	});
	$nextPage.find('.icon-next').click(function() {
		var skip = parseInt($searchForm.find('input.skip').val(), 10);
		var limit = parseInt($searchForm.find('input.limit').val(), 10);
		skip += limit;
		$searchForm.find('input.skip').val(skip);
		$searchForm.find('input.limit').val(limit);
		PARAM = $('form.search-form').serialize();
		getOrders(PARAM, function(err, result) {
			RESULT = result;
			renderList();
		});
	});

}
$(init);
